const mongoose = require('mongoose');

const hostname = 'localhost';
const port = 27017;
const dbname = 'movies';

const DB_URL = `mongodb://${hostname}:${port}/${dbname}`;

// 连接数据库
mongoose.connect(DB_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
}, err => {
    if (err) {
        console.log(err);
        throw err;
    } else {
        console.log('数据库连接成功');
    }
});


/**
 *  连接成功
 */
mongoose.connection.on('connected', () => {
    console.log('Mongoose connection open to ' + DB_URL);
})

/**
 *  连接异常
 */
mongoose.connection.on('error', (err) => {
    console.log('Mongoose connection error: ' + err);
})

/**
 *  连接断开
 */
mongoose.connection.on('disconnected', () => {
    console.log('Mongoose connection disconnected.');
})
